package com.jht.bletool2java.characteristic.userdata;

import android.bluetooth.BluetoothGattCharacteristic;
import android.util.Log;


import androidx.annotation.Keep;


import com.jht.bletool2java.characteristic.TranslateData;


import java.nio.charset.StandardCharsets;

import top.codestudy.annotation_uuid.MyUUID;

@Keep
@MyUUID(uuid = "00002a8a-0000-1000-8000-00805f9b34fb")
public class FirstName implements TranslateData {
    private static final String TAG = "FirstName";
    private String firstName = "";
    private byte[] value;
    private boolean isInvalidData = false;
    public FirstName() {
    }

    public FirstName(BluetoothGattCharacteristic characteristic) {
        if (characteristic == null){
            Log.w(TAG, "FirstName: characteristic is null!");
            isInvalidData = true;
            return;
        }
        value = characteristic.getValue();
        isInvalidData = value == null;
        if (!isInvalidData) {
            //数据合法
            firstName  = new String(value, StandardCharsets.UTF_8);
        }
    }

    public FirstName(byte[] value) {
        this.value = value;
        isInvalidData = value == null;
        if (!isInvalidData) {
            //数据合法
            firstName  = new String(value, StandardCharsets.UTF_8);
        }
    }

    @Override
    public String convert2String() {
        return "First Name: " + firstName+"\n";
    }

    @Override
    public String merge(TranslateData translateData) {
        return null;
    }
    @Override
    public boolean hasMoreData() {
        return false;
    }

    @Override
    public byte[] getData() {
        return value;
    }

    @Override
    public boolean isInvalidData() {
        return isInvalidData;
    }

    @Override
    public void parseData(BluetoothGattCharacteristic characteristic, byte[] value) {
        isInvalidData = value == null;
        if (isInvalidData) {
            //数据非法
            this.value = null;
            firstName = "";
            Log.w(TAG, "parseData: value == null");
        }else{
            this.value = value;
            firstName  = new String(value, StandardCharsets.UTF_8);
        }
    }
}
